package com.chilli.cosima.action.usermanager;

import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.chilli.cosima.action.BaseSupport;
import com.chilli.cosima.common.BussinessException;
import com.chilli.cosima.data.bean.TPower;
import com.chilli.cosima.data.bean.TRole;
import com.chilli.cosima.service.UserService;
import com.opensymphony.xwork2.ActionContext;

public class RoleAddAction extends BaseSupport
{
	/**
	 * 注释内容
	 */
	private static final long serialVersionUID = 4663886657657933847L;

	private UserService userService;

	private TRole role;

	String[] testCheck;

	public String execute()
	{
		try
		{

			userService.createRoleSavePower(role, testCheck);

			return SUCCESS;
		} catch (BussinessException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e)
		{
			e.printStackTrace();
		}
		return JUMP_PAGE;
	}

	public String jumpPage()
	{
		List<TPower> powerlist = userService.findAllPowers();

		userService.clearPowerTreeList();
		userService.createTree("0", powerlist);
		List<String> powerTreeList = userService.getPowerTreeList();

		super.request.setAttribute("powerTreeList", powerTreeList);
		return JUMP_PAGE;
	}

	public void checkRole() throws IOException
	{
		ActionContext ctx = ActionContext.getContext();

		HttpServletResponse response = (HttpServletResponse) ctx
				.get(ServletActionContext.HTTP_RESPONSE);
		OutputStream out = response.getOutputStream();

		String username = super.request.getParameter("value");
		if (username != null)
		{
			TRole role = userService.findRoleByName(username);
			if (role != null)
			{
				out.write("该名称已经被使用。".getBytes("UTF-8"));
			} else
			{
				out.write("success".getBytes("UTF-8"));
			}

		} else
		{
			out.write("该名称已经被使用。".getBytes("UTF-8"));
		}
	}

	public UserService getUserService()
	{
		return userService;
	}

	public void setUserService(UserService userService)
	{
		this.userService = userService;
	}

	public TRole getRole()
	{
		return role;
	}

	public void setRole(TRole role)
	{
		this.role = role;
	}

	public String[] getTestCheck()
	{
		return testCheck;
	}

	public void setTestCheck(String[] testCheck)
	{
		this.testCheck = testCheck;
	}

}
